//
//  String+Extention.swift
//  Weibo
//
//  Created by 李亚乐 on 15/12/28.
//  Copyright © 2015年 李亚乐. All rights reserved.
//

import Foundation

extension String {
    //"<a href=\"http://weibo.com/\" rel=\"nofollow\">微博 weibo.com</a>"
    
    func href() -> (link: String, text: String)? {
        
        let pattern = "<a href=\"(.*?)\".*?>(.*?)</a>"
        
        let regularExpression = try! NSRegularExpression(pattern: pattern, options: [])
        guard let checkingResult = regularExpression.firstMatchInString(self,
            options: [],
            range: NSRange(location: 0, length: self.characters.count)) else {
                QL1("没有匹配到内容")
                return nil
        }
        
        let str = self as NSString
        let textRange = checkingResult.rangeAtIndex(2)
        let linkRange = checkingResult.rangeAtIndex(1)
        let text = str.substringWithRange(textRange)
        let link = str.substringWithRange(linkRange)
        
        return (link, text)
    }
}